//
//  QuoteCell.m
//  Zhiche-Carriage
//
//  Created by 王亚陆 on 16/6/14.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "QuoteCell.h"
#import <Masonry.h>
@implementation QuoteCell

- (void)awakeFromNib {
    [super awakeFromNib];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        
        self.view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 222*kHeight)];
        self.contentView.backgroundColor = GrayColor;
        self.view.backgroundColor = WhiteColor;
        [self.contentView addSubview:self.view];
        
        self.number = [self createUIlabel:@"运单号:" andFont:FontOfSize12 andColor:littleBlackColor];
        self.orderNumber = [self createUIlabel:@"" andFont:FontOfSize12 andColor:littleBlackColor];
//        self.timeImage = [[UIImageView alloc]init];
//        self.timeImage.image = [UIImage imageNamed:@"quote_cell_time"];
        
//        self.time = [self createUIlabel:@"" andFont:FontOfSize14 andColor:BlackColor];
//        self.time.textColor = RGBACOLOR(254, 110, 19, 1);
        _rightLabel = [self createUIlabel:@"" andFont:FontOfSize12 andColor:YellowColor];
        _rightLabel.hidden = YES;
        [self.view addSubview:_rightLabel];
        [self.view addSubview:self.number];
        [self.view addSubview:self.orderNumber];
//        [self.view addSubview:self.time];
//        [self.view addSubview:self.timeImage];
        [self.number mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.view.mas_left).with.offset(18*kWidth);
            make.top.mas_equalTo(self.view.mas_top).with.offset(15*kHeight);
        }];
        [self.orderNumber mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.number.mas_right).with.offset(10*kWidth);
            make.top.mas_equalTo(self.view.mas_top).with.offset(15*kHeight);
        }];
        [_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view.mas_right).with.offset(-15*kWidth);
            make.centerY.mas_equalTo(self.orderNumber.mas_centerY);
        }];

//        [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(self.view.mas_right).with.offset(-14*kWidth);
//            make.top.mas_equalTo(self.view.mas_top).with.offset(15*kHeight);
//        }];
//        [self.timeImage mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(self.time.mas_left).with.offset(-7*kWidth);
//            make.centerY.mas_equalTo(self.time.mas_centerY);
//            make.size.mas_equalTo(CGSizeMake(16*kWidth, 16*kHeight));
//        }];
        
        self.carTypeAndNumber = [self createUIlabel:@"" andFont:FontOfSize13 andColor:littleBlackColor];

//        self.scrollView = [[UIScrollView alloc]init];
//        self.scrollView.contentSize = CGSizeMake(screenWidth + 150*2*kWidth, 20*kHeight);
//        self.scrollView.alwaysBounceHorizontal = YES;
//        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:self.carTypeAndNumber];
        self.carStyle = [self createUIlabel:@"" andFont:FontOfSize12 andColor:carScrollColor];
//        [self.scrollView addSubview:self.carStyle];
        //省市
        self.startAddress = [self createUIlabel:@"" andFont:FontOfSize13 andColor:littleBlackColor];
        //发车单位 收车单位  长沙芒果库
//        self.startDetailAddress = [self createUIlabel:@"" andFont:FontOfSize10 andColor:littleBlackColor];
        self.startAddress.numberOfLines = 2;
        self.startTimeL = [self createUIlabel:@"" andFont:FontOfSize10 andColor:littleBlackColor];
        self.endAddress = [self createUIlabel:@"" andFont:FontOfSize13 andColor:littleBlackColor];
        //湖南益阳宝马4S店
//        self.endDetailAddress = [self createUIlabel:@"" andFont:FontOfSize10 andColor:littleBlackColor];
        self.endAddress.numberOfLines = 2;
        self.endTimeL = [self createUIlabel:@"" andFont:FontOfSize10 andColor:littleBlackColor];
        [self.view addSubview:self.startAddress];
//        [self.view addSubview:self.startDetailAddress];
        [self.view addSubview:self.startTimeL];
        [self.view addSubview:self.endAddress];
//        [self.view addSubview:self.endDetailAddress];
        [self.view addSubview:self.endTimeL];
        
        UIImageView * imageLogo = [[UIImageView alloc]init];
        imageLogo.image = [UIImage imageNamed:@"Quote_Cell_logo"];
        [self.view addSubview:imageLogo];
        [imageLogo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.centerY.mas_equalTo(self.view.mas_top).with.offset(90*kHeight);
        }];
        
        UIView * viewline = [[UIView alloc]init];
        viewline.backgroundColor = LineGrayColor;
        [self.view addSubview:viewline];
        [viewline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view.mas_top).with.offset(45*kHeight);
            make.left.mas_equalTo(self.view.mas_left);
            make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
        }];

        [self.startTimeL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view.mas_left).with.offset(53*kHeight);
            make.top.mas_equalTo(self.startAddress.mas_bottom).with.offset(9*kHeight);
        }];
        [self.endTimeL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view.mas_right).with.offset(-56*kHeight);
            make.centerY.mas_equalTo(self.startTimeL.mas_centerY);
        }];
        
        [self.startAddress mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.startTimeL.mas_centerX);
            make.top.mas_equalTo(self.view.mas_top).with.offset(70*kHeight);
            make.width.mas_equalTo(90*kWidth);
        }];
        
        
        [self.endAddress mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.endTimeL.mas_centerX);
            make.centerY.mas_equalTo(self.startAddress.mas_centerY);
            make.width.mas_equalTo(90*kWidth);

        }];
        
        
//        [self.startDetailAddress mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.mas_equalTo(self.startAddress.mas_centerX);
//            make.top.mas_equalTo(self.startAddress.mas_bottom).with.offset(9*kHeight);
//            make.width.mas_equalTo(90*kWidth);
//            
//        }];
        
//        [self.endDetailAddress mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.mas_equalTo(self.endAddress.mas_centerX);
//            make.top.mas_equalTo(self.endAddress.mas_bottom).with.offset(9*kHeight);
//            make.width.mas_equalTo(90*kWidth);
//            
//        }];
        
        
        
        _Btn3 = [self createBtn:@"我要接单" andbackcolor:YellowColor andbordercolor:YellowColor andtitlecolor:WhiteColor];
        _line1=[[UIView alloc]init];
        _line1.backgroundColor = LineGrayColor;
        [self.view addSubview:_line1];
        _line2=[[UIView alloc]init];
        _line2.backgroundColor = LineGrayColor;
        [self.view addSubview:_line2];
        
        
        _priceLabel = [self createUIlabel:@"￥0.00" andFont:FontOfSize13 andColor:YellowColor];
        _priceLabel.textAlignment = NSTextAlignmentLeft;
//        _total = [self createUIlabel:@"￥1500.99" andFont:FontOfSize12 andColor:YellowColor];
//        
//        _unLabel = [self createUIlabel:@"运费" andFont:FontOfSize12 andColor:fontGrayColor];
//        _toLabel = [self createUIlabel:@"总报价" andFont:FontOfSize12 andColor:fontGrayColor];
//        _viewLine = [[UIView alloc]init];
//        _viewLine.backgroundColor = GrayColor;
//        [self.view addSubview:_viewLine];
        [self.view addSubview:_priceLabel];
//        [self.view addSubview:_toLabel];
//        [self.view addSubview:_unitPrice];
//        [self.view addSubview:_total];

//        [_unitPrice mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(_unLabel.mas_right).with.offset(5*kWidth);
//            make.centerY.mas_equalTo(_unLabel.mas_centerY);
//        }];
//        [_viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(_unLabel.mas_top);
//            make.bottom.mas_equalTo(_unLabel.mas_bottom);
//            make.left.mas_equalTo(_unitPrice.mas_right).with.offset(8*kWidth);
//            make.width.mas_equalTo(0.5);
//        }];
//        [_toLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(_viewLine.mas_right).with.offset(5*kWidth);
//            make.centerY.mas_equalTo(_unLabel.mas_centerY);
//        }];
//        [_total mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(_toLabel.mas_right).with.offset(5*kWidth);
//            make.centerY.mas_equalTo(_unLabel.mas_centerY);
//        }];
    }
    return self;
}

-(void)showInfo:(NSMutableDictionary *)dic_info andType:(int)intType{
    
//        arriveDate = "2016-09-23";
//        bidderType = "<null>";
//        bidderTypeText = "";
//        cost = 333373;
//        deliverPics = "<null>";
//        departAddr = "\U7687\U540e\U5927\U9053\U4e1c";
//        departCityName = "\U90a2\U53f0\U5e02";
//        departContact = "<null>";
//        departCountyName = "<null>";
//        departPhone = "<null>";
//        departProvinceName = "\U6cb3\U5317\U7701";
//        detail = "2\U4e2a\U54c1\U724c 2\U8f86\U8f66";
//        driverInfo = "<null>";
//        id = 105;
//        payStatus = 10;
//        payStatusText = "\U672a\U4ed8\U6b3e";
//        postTimeRemaining = 0;
//        receiptAddr = "\U7687\U540e\U5927\U9053\U897f";
//        receiptCityName = "\U90af\U90f8\U5e02";
//        receiptContact = "<null>";
//        receiptCountyName = "<null>";
//        receiptPhone = "<null>";
//        receiptProvinceName = "\U6cb3\U5317\U7701";
//        sendPics = "<null>";
//        shipmentDate = "2016-09-22";
//        status = 10;
//        statusText = "\U5f85\U63a5\U5355";
//        vehicles =     (
//                        {
//                            brand =             {
//                                id = 3;
//                                logo = "Audi.png";
//                            };
//                            brandId = 3;
//                            brandLogo = "<null>";
//                            brandName = "\U5965\U8fea";
//                            id = 147;
//                            vehicleId = 11;
//                            vehicleName = "\U5965\U8fea100";
//                            vin = 11111121111111111;
//                        },
//                        {
//                            brand =             {
//                                id = 4;
//                                logo = "<null>";
//                            };
//                            brandId = 4;
//                            brandLogo = "<null>";
//                            brandName = "\U5b89\U51ef\U5ba2\U8f66";
//                            id = 148;
//                            vehicleId = 55;
//                            vehicleName = "\U5b9d\U65af\U901a";
//                            vin = 11123123123123123;
//                        }
//                        );
//        waybillCode = WB778157093414678528;
    
    
    NSString * shipp = [NSString stringWithFormat:@"%@",[self backString:dic_info[@"cost"]]];
    float shipping = [shipp floatValue];
    self.priceLabel.text = [NSString stringWithFormat:@"运费:￥%.2f",shipping];
    
    if (intType ==1) {
//        [_priceLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(self.view.mas_left).with.offset(18*kWidth);
//            make.centerY.mas_equalTo(_line2.mas_bottom).with.offset(21.5*kHeight);
//        }];
        
        _priceLabel.textAlignment = NSTextAlignmentLeft;
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.view.mas_left).with.offset(18*kWidth);
            make.centerY.mas_equalTo(_line2.mas_bottom).with.offset(21.5*kHeight);
        }];

        _Btn3.hidden = NO;
        
    }else{
        _Btn3.hidden = YES;
        _priceLabel.textAlignment = NSTextAlignmentRight;

//        [_priceLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(self.view.mas_right).with.offset(-15*kWidth);
//            make.centerY.mas_equalTo(_line2.mas_bottom).with.offset(21.5*kHeight);
//        }];
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.view.mas_right).with.offset(-15*kWidth);
            make.centerY.mas_equalTo(_line2.mas_bottom).with.offset(21.5*kHeight);
        }];

    }

    
    self.orderNumber.text = [NSString stringWithFormat:@"%@",[self backString:dic_info[@"waybillCode"]]];
//    self.time.text =[NSString stringWithFormat:@"%@分钟",[self backString:dic_info[@"postTimeRemaining"]]];
    NSString * loadProvince = [NSString stringWithFormat:@"%@",[self backString:dic_info[@"departProvinceName"]]];
    NSString * loadCity = [NSString stringWithFormat:@"%@",[self backString:dic_info[@"departCityName"]]];
    
    NSString * startAddress = nil;
    if ([loadCity isEqualToString:loadProvince]) {
        startAddress = [NSString stringWithFormat:@"%@",loadCity];
    }else{
        startAddress = [NSString stringWithFormat:@"%@-%@",loadProvince,loadCity];
    }
    self.startAddress.text = startAddress;//省市
//    self.startDetailAddress.text = [NSString stringWithFormat:@"%@",dic_info[@"departUnit"]];//发车单位
    self.startTimeL.text = [NSString stringWithFormat:@"%@", [self backString:dic_info[@"shipmentDate"]]];//发车时间
    NSString * unloadProvince = [NSString stringWithFormat:@"%@",[self backString:dic_info[@"receiptProvinceName"]]];
    NSString * unloadCity = [NSString stringWithFormat:@"%@",[self backString:dic_info[@"receiptCityName"]]];
    
    NSString * endAddress = nil;
    if ([unloadProvince isEqualToString:unloadCity]) {
        endAddress = [NSString stringWithFormat:@"%@",unloadProvince];
    }else{
        endAddress = [NSString stringWithFormat:@"%@-%@",unloadProvince,unloadCity];
    }

    self.endAddress.text = endAddress;//省市县
//    self.endDetailAddress.text =  [NSString stringWithFormat:@"%@",dic_info[@"receiptUnit"]];//收车单位
    
//    NSLog(@"%f",self.endAddress.frame.size.width);
    self.endTimeL.text = [NSString stringWithFormat:@"%@", [self backString:dic_info[@"arriveDate"]]];//到达时间
    
    [_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
        make.top.mas_equalTo(self.view.mas_top).with.offset(135*kHeight);
    }];
    [self.carTypeAndNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_line1.mas_bottom).offset(21.5*kHeight);
        make.left.mas_equalTo(self.view.mas_left).with.offset(18*kWidth);
    }];
    
    [_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
        make.top.mas_equalTo(self.carTypeAndNumber.mas_centerY).with.offset(21.5*kHeight);
    }];
    
    //detail
    NSMutableArray * carstyle = dic_info[@"vehicles"];
    NSString * carName = [[NSString alloc]init];
    for (int i=0; i<carstyle.count; i++) {
        carName = [carName stringByAppendingString:[NSString stringWithFormat:@"%@-%@-%@辆   ",carstyle[i][@"brandName"],carstyle[i][@"vehicleName"],carstyle[i][@"amount"]]];
    }
    self.carTypeAndNumber.text = dic_info[@"detail"];
//    self.carStyle.text = carName;
//    self.carStyle.textAlignment = NSTextAlignmentLeft;
//    [self.carStyle mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.scrollView.mas_left).with.offset(18*kWidth);
//        make.centerY.mas_equalTo(self.scrollView.mas_centerY);
//    }];
//    self.scrollView.contentSize = CGSizeMake(self.carStyle.frame.size.width, 20*kHeight);
    [self.view addSubview:_Btn2];
    [self.view addSubview:_Btn3];
    
    [_Btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).with.offset(-14*kWidth);
        make.size.mas_equalTo(CGSizeMake(70*kWidth, 30*kHeight));
        make.centerY.mas_equalTo(self.priceLabel.mas_centerY);
    }];
    

}

-(NewBtn*)createBtn:(NSString*)title andbackcolor:(UIColor*)backcolor andbordercolor:(UIColor*)bordercolor andtitlecolor:(UIColor*)titlecolor {
    
    NewBtn * btn = [NewBtn buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titlecolor forState:UIControlStateNormal];
    btn.titleLabel.font = Font(FontOfSize14);
    btn.backgroundColor = backcolor;
    
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 5;
    btn.layer.borderColor = bordercolor.CGColor;
    //    btn.layer.borderColor = RGBACOLOR(149, 149, 149, 1).CGColor;
    
    return btn;
}


-(UILabel*)createUIlabel:(NSString *)title andFont:(CGFloat)font andColor:(UIColor*)color {
    UILabel * label =[[UILabel alloc]init];
    label.text = title;
    label.textColor = color;
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:font];CGSize size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    CGFloat nameH = size.height;
    CGFloat nameW = size.width;
    label.textAlignment = NSTextAlignmentCenter;
    label.frame =CGRectMake(0, 0, nameW, nameH);
    label.font = Font(font);
    return label;
}

-(NSString *)backString:(NSString *)string
{
    //    NSString * str = [NSString stringWithFormat:@"%@",string];
    
    if ([string isEqual:[NSNull null]]) {
        return @"";
    } else {
        
        return string;
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
